<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of Melange_Framework
 *
 * PHP-MVC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MVC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MVC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package mvc
 *
 */
/**
 *
 */
require_once("mvc/action/ActionMessage.php");

/**
 * PHP_MVC_View_Form_Helper_Errors class
 *
 * Helper to generate a "errors"
 *
 * @category    Melange Framework
 * @package     mvc
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class PHP_MVC_View_Form_Helper_Errors {

    public function errors($bundle=null, $args=null, $localeKey=null)
    {
        $actionErrors = $this->pageContext->request->getAttribute(MVC::ERROR_KEY);

        if(is_null($actionErrors))
            return;

        if(!$actionErrors->isEmpty()) { ?>

            <p class="error"><?=TagUtils::message($this->pageContext, $bundle, $localeKey, "error.message", $args)?></p>

            <ul class="error">
            <?
            $messages = $actionErrors->get();
            while(list($key, $value) = each($messages)) {

                foreach($value as $actionError) {

                    if($actionError->isResource()) {
                        // Retrieve the message string we are looking for
                        $message = TagUtils::message($this->pageContext, $bundle,
                                $localeKey, $actionError->getKey(), is_null($args) ? $actionError->getValues() : $args);
                    } else
                        $message = $actionError->getKey();

                    ?>

                    <? if($key == ActionMessage::GLOBAL_MESSAGE) { ?>
                        <li><?=$message?></li>
                    <? } else { ?>
                        <li><a href="javascript:try { ito(document.getElementById('<?=htmlentities(str_replace("->", "_", $key))?>')) } catch(e) {}" onclick="try { document.getElementById('<?=htmlentities(str_replace("->", "_", $key))?>').focus(); } catch(e) {}"><?=$message?></a></li>
                    <? }
                }
            } ?>
            </ul>
        <? }
    }
}
?>